package com.itheima.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.entry.PageResult;
import com.itheima.entry.QueryPageBean;
import com.itheima.mapper.MenuMapper;
import com.itheima.mapper.UserMapper;
import com.itheima.pojo.Menu;
import com.itheima.pojo.User;
import com.itheima.service.MenuService;
import com.itheima.service.UserService;
import com.itheima.vo.MenuVo;
import org.apache.dubbo.config.annotation.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author jshstart
 * @creat 2020-11-23 9:43
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {
    @Override
    public PageResult queryAllByPage(QueryPageBean queryPageBean) {
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete",0);
        Page page=this.page(new Page<Menu>(queryPageBean.getCurrentPage(),queryPageBean.getPageSize()),queryWrapper);
        return new PageResult(page.getTotal(),page.getRecords());
    }

    @Override
    public void deleteById(Long id) {
        Menu menu = new Menu();
        menu.setId(id);
        menu.setIsDelete(1);
        this.updateById(menu);
    }

    @Override
    public List<Menu> findAllMenu() {
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        List<Menu> list = this.list(queryWrapper);
        return list;
    }
    @Override
    public List<MenuVo> findMenu(String name ) {
        int i=0;
        List<MenuVo> list = new ArrayList<>();
        List<Menu> parent = baseMapper.findParent();
        System.out.println("parent="+parent);
        for (Menu menu : parent) {
            i++;
            MenuVo menuVo = new MenuVo();
            menuVo.setId(menu.getId());
            menuVo.setPath(menu.getPath());
            menuVo.setTitle(menu.getTitle());
            menuVo.setIcon(menu.getIcon());
            menuVo.setLinkUrl(menu.getLinkUrl());
            menuVo.setPagePath(menu.getPagePath());
            List<Menu>  children  = baseMapper.findChildren(menu.getId());
            if(name.equals("焦世辉")){
                menuVo.setChildren(children);
                list.add(menuVo);
            }
            if(name.equals("user2")&&i==4||name.equals("user2")&&i==1||name.equals("user2")&&i==3){
                menuVo.setChildren(children);
                list.add(menuVo);
            }

        }
        return list;

    }

}
